package com.xdtech.sys.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.xdtech.common.exception.BusinessException;
import com.xdtech.common.utils.ApplicationContextUtil;
import com.xdtech.sys.aspect.SystemControllerLog;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.sys.service.ParamsService;
import com.xdtech.sys.util.SysConstants;
import com.xdtech.sys.vo.ParamsItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-05-31 22:43:28
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class ParamsController {
	private Log log = LogFactory.getLog(ParamsController.class);
	@Autowired
	private ParamsService paramsService;
	@RequestMapping(value="/params.do",params = "params")
	public ModelAndView skipParams() {
		return new ModelAndView("sys/params/params_ftl");
	}
	
	@RequestMapping(value="/params.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(ParamsItem item,Pagination pg) {
		Map<String, Object> results =  paramsService.loadPageDataByConditions(pg,item,"findParamsByCondition");
		return results;
	}
	
	@RequestMapping(value="/params.do",params = "editParams")
	@SystemControllerLog(description = "编辑")
	public ModelAndView editParams(HttpServletRequest request,Long paramsId) {
		if (paramsId!=null) {
			request.setAttribute("paramsItem", paramsService.loadParamsItem(paramsId));
		}
		return new ModelAndView("sys/params/editParams_ftl");
	}
	
	@RequestMapping(value="/params.do",params = "saveParams")
	@ResponseBody
	@SystemControllerLog(description = "保存")
	public ResultMessage saveParams(ParamsItem item) {
		ResultMessage r = new ResultMessage();
		if (paramsService.saveOrUpdateParams(item)) {
			if (SysConstants.YES.equals(item.getInitToApp()))
			{
				ApplicationContextUtil.getApplication().setAttribute(item.getCode(), item.getCodeValue());
			}else
			{
				ApplicationContextUtil.getApplication().removeAttribute(item.getCode());
			}
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
//		throw new BusinessException();
	}
	
	@RequestMapping(value="/params.do",params = "deleteParamsItems")
	@ResponseBody
	@SystemControllerLog(description = "删除")
	public ResultMessage deleteParamsItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> paramsIds = new ArrayList<Long>();
			for (String id : tempIds) {
				paramsIds.add(Long.valueOf(id));
			}
			paramsService.deleteParamsInfo(paramsIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}

}
